<template>
  <view class="container">
    <u-navbar
      title="资讯详情"
      :is-back="true"
    ></u-navbar>
    
    <view class="news-detail">
      <view class="news-header">
        <view class="news-title">{{ newsDetail.title }}</view>
        <view class="news-meta">
          <text class="news-time">{{ newsDetail.create_time }}</text>
          <text class="news-views">{{ newsDetail.view_count }} 阅读</text>
        </view>
      </view>
      
      <view class="news-cover" v-if="newsDetail.cover_image">
        <image :src="newsDetail.cover_image" mode="widthFix"></image>
      </view>
      
      <view class="news-content">
        <rich-text :nodes="newsDetail.content"></rich-text>
      </view>
    </view>
    
    <view class="related-news" v-if="relatedNews.length > 0">
      <view class="section-title">相关资讯</view>
      <view class="related-list">
        <view 
          class="related-item" 
          v-for="(item, index) in relatedNews" 
          :key="index"
          @click="goToDetail(item.id)"
        >
          <text class="dot"></text>
          <text class="related-title">{{ item.title }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      newsDetail: {
        title: '',
        content: '',
        cover_image: '',
        create_time: '',
        view_count: 0
      },
      relatedNews: []
    }
  },
  onLoad(options) {
    this.id = options.id || ''
    
    if (this.id) {
      this.getNewsDetail()
    } else {
      uni.showToast({
        title: '参数错误',
        icon: 'none'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }
  },
  methods: {
    // 获取资讯详情
    async getNewsDetail() {
      try {
        uni.showLoading({
          title: '加载中'
        })
        
        const res = await this.$api.getNewsDetail({
          id: this.id
        })
        
        uni.hideLoading()
        
        if (res.data && res.data.code === 200) {
          this.newsDetail = res.data.data.detail || {}
          this.relatedNews = res.data.data.related || []
          
          // 设置页面标题
          uni.setNavigationBarTitle({
            title: this.newsDetail.title || '资讯详情'
          })
        } else {
          uni.showToast({
            title: res.data.message || '获取资讯详情失败',
            icon: 'none'
          })
        }
      } catch (error) {
        uni.hideLoading()
        console.error('获取资讯详情失败', error)
        uni.showToast({
          title: '获取资讯详情失败',
          icon: 'none'
        })
      }
    },
    
    // 跳转到其他详情页
    goToDetail(id) {
      // 如果是当前页面，不跳转
      if (id === this.id) return
      
      uni.navigateTo({
        url: `/pages/news/detail?id=${id}`
      })
    }
  },
  // 分享功能
  onShareAppMessage() {
    return {
      title: this.newsDetail.title,
      path: `/pages/news/detail?id=${this.id}`
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #ffffff;
  padding-bottom: 30rpx;
}

.news-detail {
  padding: 20rpx;
}

.news-header {
  margin-bottom: 30rpx;
}

.news-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
  margin-bottom: 20rpx;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  font-size: 24rpx;
  color: #999;
}

.news-cover {
  margin-bottom: 30rpx;
  
  image {
    width: 100%;
    border-radius: 8rpx;
  }
}

.news-content {
  font-size: 28rpx;
  color: #333;
  line-height: 1.6;
  
  :deep(img) {
    max-width: 100%;
    height: auto;
  }
  
  :deep(p) {
    margin-bottom: 20rpx;
  }
}

.related-news {
  margin-top: 40rpx;
  padding: 0 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  position: relative;
  padding-left: 20rpx;
  
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 6rpx;
    height: 30rpx;
    background-color: #1A8DFF;
    border-radius: 3rpx;
  }
}

.related-list {
  padding: 0 10rpx;
}

.related-item {
  display: flex;
  align-items: center;
  padding: 16rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
  
  &:last-child {
    border-bottom: none;
  }
  
  .dot {
    width: 12rpx;
    height: 12rpx;
    border-radius: 50%;
    background-color: #1A8DFF;
    margin-right: 16rpx;
  }
  
  .related-title {
    flex: 1;
    font-size: 28rpx;
    color: #333;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }
}
</style>
